package com.ywk.base.designPattern.observer.myObserver;

import java.util.ArrayList;
import java.util.List;

/**
 * 具体被观察者
 * @Author zbh
 * @Date 2023/4/3
 */
public class ConcreteSubject implements Subject{

    private String name;

    // 存放观察者的集合
    private List<Observer> observers;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public ConcreteSubject(String name){
        this.name=name;
        observers = new ArrayList<>();
    }
    // 注册观察者
    @Override
    public void registerObserver(Observer o){
        if(!observers.contains(o)){
            observers.add(o);
        }
    }
    // 取消注册观察者
    @Override
    public void unregisterObserver(Observer o){
        if(observers.contains(o)){
            observers.remove(o);
        }
    }
    @Override
    public void notifyObserver(){
        System.out.println("送奶工"+name+"前往送奶");
        for(int i=0;i<observers.size();i++){
            observers.get(i).update();
        }
    }
}
